package cn.edu.njpi.trip;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @author 朱宗辉
 * @version V1.0
 * @Package cn.edu.njpi.trip
 * @date 2021/12/12 上午1:27
 */
public class TripMapper extends Mapper<LongWritable, Text, Text, Text> {
    private Text outK = new Text();
    private Text outV = new Text();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 获取一行数据，转成字符串
        String line = value.toString();

        // 切割数据
        String[] split = line.split("\",\"");

        // 获取我们需要的数据
        // 支付宝账号
        String id = split[0].substring(1);
        // 发烧状态
        String state = split[3];
        // 到达的地区
        String trips = split[5].replace("\"", "");
        String color = "";
        if (!state.contains("正常") && (trips.contains("扬州") || trips.contains("镇江"))) {
            color = "red";
        } else if (state.contains("正常") && (trips.contains("扬州") || trips.contains("镇江"))) {
            color = "yellow";
        } else {
            color = "green";
        }
        // 封装
        outK.set(id);
        outV.set(color);
        // 输出
        context.write(outK, outV);
    }

    public static String transformText(String text, String encoding) {
        String value = null;
        try {
            value = new String(text.getBytes(), 0, text.length(), encoding);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return value;
    }
}
